/**
 * Created by cwj on 2017/5/4.
 */
define(['app', 'filters/filters'], function (app) {
    app.register.controller('CircuLarize', function ($scope, $http, $state, $sce, $stateParams, $rootScope, $timeout,$q) {
        $scope.sce = $sce.trustAsResourceUrl;
        $scope.loading_show = true;
        $scope.id = $stateParams.id;
        $scope.tipModal = {
            isShow: false,
            title: '温馨提示',
            data: {},
            confirm: function() {
                this.hide()
            },
            cancel: function() {
                this.hide()
            },
            hide: function() {
                this.isShow = false;
            },
            show: function() {
                this.isShow = true;
            }
        }
        if($rootScope.num==0){//是否是企业用户
            $scope.tipModal.show();
            $rootScope.num=1;
        }
        //    -------------------------通知
        //给页面一个鼠标弹起事件
        // $(document).mouseup(function (e) {//$(e.target).parents(".pull_down").length==0;点击头像为0,1，点击其他为0,0
        //     if ($(e.target).parents(".pull_down").length == 0 && $(e.target).parents(".userimg").length == 0) {
        //         $(".pull_down").hide();
        //         $scope.flag = false;
        //     } else {
        //         $(".pull_down").show();
        //     }
        //     if ($(e.target).parents(".pull_Down").context.className != 'pull_Down' || $(e.target).parents(".pitch_mes_li").context.className == 'pitch_mes_li' || $(e.target).parents(".pitch_mes_li").context.className == 'pitch_mes_li pitch_mes') {//点击的不是通知按钮
        //         $(".pull_Down_next").hide();
        //         $scope.uiModel.is_pull_Down_next = false;
        //     } else {
        //         $scope.uiModel.is_pull_Down_next = false;
        //         $(".pull_Down_next").show();
        //     }
        // })
        // 通知栏点击
        var con_l = document.getElementsByClassName('con_l3')[0];
        con_l.style.height = document.body.clientHeight - 140 + 'px';
        // if (con_l.scrollHeight > con_l.offsetHeight || (con_l.scrollHeight == con_l.offsetHeight && con_l.offsetHeight != 0)) {
        //     con_l.style.width = '346px';
        // }
        // 检测拖动条是否已经达到底部，是否需要加载新数据
        $(".con_l3").scroll(function () {
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $(this)[0].scrollHeight;
            var windowHeight = $(this).height();
            if (scrollTop + windowHeight == scrollHeight) {
                if ($scope.uiModel.listRequest.page < $scope.uiModel.pageTotal) {
                    $scope.uiModel.showLoading = true;
                    $('.loading').removeClass('ng-hide');
                    $(this).scrollHeight = scrollHeight + 60;
                    $(this).scrollTop(scrollTop + 60);
                    $scope.loadMore();
                }
            }
        })
        // $(document).mouseup(function (e) {
        //     if (($(e.target).context.className != 'pull_Down_l' && $(e.target).parents('.pull_Down_l').length == 0) && ($(e.target).context.className != 'pull_Down_r' && $(e.target).parents('.pull_Down_r').length == 0)) {
        //         $timeout(function () {
        //             $scope.uiModel.is_pull_Down_next = false;
        //             $scope.uiModel.showExt = false;
        //         }, 0);
        //     }
        // });
        $scope.uiModel = {
            is_pull_Down_next: false,//通知下拉显示隐藏
            is_mes: true,//默认true选全部消息
            readAll: false,
            id: 0, // 通知选定的
            dataList: [],
            detail: {},
            ListConfig: {
                pageSize: 10,
                pageIndex: 1,
                totalCount: 0,
            },
            listRequest: { message_filter: 0, page: 0 },
            showErrorTip: false,  //是否显示错误弹窗
            showLoading: false,
            showEntire: false,
            showDetail: false,
            showExt: false,
            pageTotal: 0,
            // dataList: []
        }
        //通知一个点击事件
        $scope.pull_down_click = function () {
            $scope.uiModel.showExt = false;
            $scope.uiModel.is_pull_Down_next = !$scope.uiModel.is_pull_Down_next;
        }
        //全部消息、未读消息li点击事件
        $scope.mes_click_all = function () {
            $scope.uiModel.is_mes = true;
            $scope.uiModel.showDetail = false;
            $scope.getList(0, 1);
        }
        $scope.mes_click_no = function () {
            $scope.uiModel.is_mes = false;
            $scope.uiModel.showDetail = false;
            $scope.getList(1, 1);
        }
        // 显示标记已读功能
        $scope.showExt = function () {
            $scope.uiModel.is_pull_Down_next = false;
            $scope.uiModel.showExt = !$scope.uiModel.showExt;
        }
        // 标记全部已读
        $scope.allRead = function () {

            $scope.uiModel.readAll = true;
            $scope.readAll();
        }
        // 初始化
        $scope.init = function () {
            $scope.getList(0, 1).then(function(){
                $scope.detail($scope.firstMessage)
            });
        }
        // 获取列表
        $scope.getList = function (type, pageIndex) {
            var deferred = $q.defer();
            $scope.uiModel.listRequest.message_filter = type;
            $scope.uiModel.listRequest.page = pageIndex;
            $scope.uiModel.showEntire = false;
            $http.post(app._baseUrl + '/Message/Message/messageList', $scope.uiModel.listRequest).success(
                function (result) {
                    $scope.loading_show = false;
                    $scope.uiModel.dataList = result.data;
                    $scope.firstMessage=result.data[0]
                    if (result.page) {
                        $scope.uiModel.pageTotal = result.page.page_total;
                        if ($scope.uiModel.listRequest.page == $scope.uiModel.pageTotal) {
                            $scope.uiModel.showEntire = true;
                        }
                        deferred.resolve(111)
                    } else {
                        $scope.uiModel.pageTotal = 0;
                        $scope.uiModel.showEntire = false;
                        deferred.reject(111)
                    }
                }
            ).error(function (result) {
                $scope.netError();
            });
            return deferred.promise
        }
        // 加载更多
        $scope.loadMore = function () {
            if ($scope.uiModel.listRequest.page < $scope.uiModel.pageTotal) {
                $scope.uiModel.listRequest.page += 1;
                $http.post(app._baseUrl + '/Message/Message/messageList', $scope.uiModel.listRequest).success(
                    function (result) {
                        $scope.uiModel.showLoading = false;
                        if (result.data.length > 0 && result.page) {
                            $scope.uiModel.dataList = $scope.uiModel.dataList.concat(result.data);
                            if ($scope.uiModel.listRequest.page == $scope.uiModel.pageTotal && $scope.uiModel.pageTotal != 0) {
                                $scope.uiModel.showEntire = true;
                            }
                        } else {
                            $scope.uiModel.showEntire = true;
                        }
                    }
                ).error(function (result) {
                    $scope.netError();
                });
            }
        };
        $scope.detail = function (item) {
            $scope.uiModel.id = item.message_id;
            $http.get(app._baseUrl + '/Message/Message/detail&message_id=' + item.message_id).success(function (result) {
                $scope.uiModel.detail = result.data;
                if (item.is_read == false) {
                    item.is_read = true;
                    $rootScope.messageCount -= 1;
                }
                $scope.uiModel.showDetail = true;
            }).error(
                function (result) {
                    $scope.netError();
                });


        }
        // 显示错误
        $scope.netError = function () {
            $timeout(function () {
                $scope.uiModel.showErrorTip = true;
            }, 0);
            $timeout(function () {
                $scope.uiModel.showErrorTip = false;
            }, 3000);
        }
        $scope.readAll = function () {
            $http.post(app._baseUrl + '/Message/Message/readAll').success(
                function (result) {
                    // 同步导航栏通知数量
                    $rootScope.messageCount = 0;
                    $scope.uiModel.showExt = false;
                    $scope.uiModel.dataList.forEach(function (v, index, array) {
                        array[index].is_read = true;
                    })
                }
            ).error(
                function (result) {
                    $scope.netError();
                })
        }
        $scope.init();
    })
})